use std::collections::HashMap;

use serde::Deserialize;

use super::{Category, Status};

#[derive(Debug, Clone, Default)]
pub struct AriaParams {
    pub uri: String,
    pub gid: String,
    pub dir: Option<String>,
    pub name: Option<String>,
    pub select_file: Option<String>,
}

impl AriaParams {
    pub fn get_uris(&self) -> Vec<String> {
        vec![self.uri.clone()]
    }
    fn get_dir_or(&self, default_dir: String) -> String {
        self.dir
            .as_ref()
            .map(|d| {
                if d.is_empty() {
                    default_dir.clone()
                } else {
                    d.clone()
                }
            })
            .unwrap_or(default_dir)
    }
    pub fn file_options(&self, default_dir: String) -> HashMap<String, String> {
        let mut map = HashMap::new();
        map.insert("dir".into(), self.get_dir_or(default_dir));
        if let Some(name) = &self.name {
            map.insert("out".into(), name.clone());
        }
        map
    }
    pub fn magnet_options(&self, default_dir: String) -> HashMap<String, String> {
        let mut map = HashMap::new();
        map.insert("dir".into(), self.get_dir_or(default_dir));
        map.insert("bt-remove-unselected-file".into(), "true".into());
        map
    }
    pub fn torrent_options(&self, default_dir: String) -> HashMap<String, String> {
        let mut map = HashMap::new();
        map.insert("dir".into(), self.get_dir_or(default_dir));
        map.insert("bt-remove-unselected-file".into(), "true".into());
        map
    }
    pub fn select_options(&self) -> HashMap<String, String> {
        let mut map = HashMap::new();
        if let Some(select_file) = &self.select_file {
            map.insert("select-file".into(), select_file.clone());
        }
        map
    }
}

#[derive(Debug, Deserialize)]
pub struct AddReq {
    pub uri: String,
    pub cate: Category,
    pub dir: Option<String>,
    pub name: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct QueryReq {
    pub status: Option<Status>,
    pub name: Option<String>,
}

#[derive(Debug, Deserialize)]
pub struct SelectFileReq {
    pub gid: String,
    pub select_file: String,
}

#[derive(Debug, Deserialize)]
pub struct ClearReq {
    pub gids: Vec<String>,
}
